package com.ctz.commonframe.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Toast;

import com.ctz.commonframe.R;
import com.ctz.commonframe.utils.Logger;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * Created by 陈堂正 on 2017/4/18.
 */

public class QRCodeScanActivity extends TitleBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode_scan);
        setTitleMiddleText("扫一扫");
        CaptureFragment captureFragment = new CaptureFragment();
        CodeUtils.setFragmentArgs(captureFragment, R.layout.layout_qrcode_scan);

        CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                Logger.e("QRCodeScanActivity", "二维码扫描结果：" + result);
                Intent data = new Intent();
                String functionName = QRCodeScanActivity.this.getIntent().getStringExtra(WebViewActivity.EXTRA_KEY_QRCODE_CALLBACK_FUNCTION_NAME);
                data.putExtra(WebViewActivity.EXTRA_KEY_QRCODE_RESULT, functionName + "('" + result + "')");
//                showLongToast(functionName + "('" + result + "')");
                setResult(WebViewActivity.REQUEST_CODE_QRCODE_SCAN, data);
                finish();
            }

            @Override
            public void onAnalyzeFailed() {
                onCreate(null);
                Toast.makeText(QRCodeScanActivity.this, "扫描失败", Toast.LENGTH_SHORT).show();
            }
        };

        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.layout_qrcode_container, captureFragment).commit();
    }
}
